#ifndef INSTANCE_H_INCLUDED
#define INSTANCE_H_INCLUDED

#include "types.h"

#define INSTANCE_LOAD(ds) \
    if (!ds) { \
        ds = DataStream::create(sClassId, getName(), Input); \
        if (ds) { \
            if (load(ds)) { DataStream::destroy(ds); return true; } \
            else { DataStream::destroy(ds); return false; } \
        } \
        else return false; \
    } \
    if(!Instance::load(ds)) \
        return false;

#define INSTANCE_SAVE(ds) \
    if (!ds) { \
        ds = DataStream::create(sClassId, getName(), Output); \
        if (ds) { \
            if (save(ds)) { DataStream::destroy(ds); return true; } \
            else { DataStream::destroy(ds); return false; } \
        } \
        else return false; \
    } \
    if(!Instance::save(ds)) \
        return false;

class Instance
{
public:
    Instance(const string& name = "Untitled", ClassId* classId = Instance::sClassId);
    virtual ~Instance();

    virtual string getName() { return mName; }
    virtual bool setName(string name);

    virtual ClassId* getClassId() { return mClassId; }

    static ClassId* sClassId;
    static Instance* sNew(const string& name = "Untitled", ClassId* classId = sClassId) { return new Instance(name, classId); }
    virtual bool load(DataStream* ds = 0);
    virtual bool save(DataStream* ds = 0);

protected:
    friend class DataStream;
    string mName;
    ClassId* mClassId;
};

#endif // INSTANCE_H_INCLUDED
